package com.ksd.web.controller.system;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ksd.common.annotation.Log;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.core.page.TableDataInfo;
import com.ksd.common.enums.BusinessType;
import com.ksd.common.enums.SourceTypeEnum;
import com.ksd.common.utils.StringUtils;
import com.ksd.common.utils.poi.ExcelUtil;
import com.ksd.system.domain.SysBoard;
import com.ksd.system.domain.SysSongExcel;
import com.ksd.system.domain.SysTvData;
import com.ksd.system.service.ISysBoardService;
import com.ksd.system.service.ISysTvDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ksd.system.domain.SysMusicData;
import com.ksd.system.service.ISysMusicDataService;

/**
 * 纯音乐享Controller
 * 
 * @author y.wu
 * @date 2025-09-05
 */
@RestController
@RequestMapping("/system/musicData")
public class SysMusicDataController extends BaseController
{
    @Autowired
    private ISysMusicDataService sysMusicDataService;
    @Autowired
    private ISysTvDataService sysTvDataService;
    @Autowired
    private ISysBoardService sysBoardService;

    /**
     * 查询纯音乐享列表
     */
    @PreAuthorize("@ss.hasPermi('system:musicData:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMusicData sysMusicData)
    {
        startPage();
        List<SysMusicData> list = sysMusicDataService.selectSysMusicDataList(sysMusicData);
        SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
        String url = sysBoard.getUrl();
        for (SysMusicData musicData : list) {
            musicData.setMusicUrl(url+musicData.getMusicUrl());
            musicData.setLyricUrl(url+musicData.getLyricUrl());
            musicData.setImgUrl(url+musicData.getImgUrl());
            SysTvData sysTvData = sysTvDataService.selectSysTvDataById(musicData.getTypeData());
            if (StringUtils.isNotNull(sysTvData)) {
                musicData.setMusicType(sysTvData.getName());
            }
        }
        return getDataTable(list);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SysSongExcel> util = new ExcelUtil<>(SysSongExcel.class);
        List<SysMusicData> sysMusicDataList = sysMusicDataService.selectSysMusicDataList(new SysMusicData());
        List<SysSongExcel> list = new ArrayList<SysSongExcel>();

        SysMusicData sysMusicData = sysMusicDataList.get(0);
        if (StringUtils.isNotNull(sysMusicData)){
            SysTvData sysTvData = sysTvDataService.selectSysTvDataById(sysMusicData.getTypeData());
            SysSongExcel sysSongExcel = new SysSongExcel();
            sysSongExcel.setTitle(sysMusicData.getTitle());
            sysSongExcel.setFileType(sysTvData.getName());
            sysSongExcel.setVideoFile(sysMusicData.getVideoAddress());
            sysSongExcel.setRecommendBanner(sysMusicData.getImgAddress());
            sysSongExcel.setSongWord(sysMusicData.getSongWord());
            sysSongExcel.setContentTitle(sysMusicData.getContentTitle());
            sysSongExcel.setSinger(sysMusicData.getSinger());
            list.add(sysSongExcel);
        }
        util.exportExcel(response,list,"歌曲模板");
    }
    /**
     * 导出纯音乐享列表
     */
    @PreAuthorize("@ss.hasPermi('system:musicData:export')")
    @Log(title = "纯音乐享", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMusicData sysMusicData)
    {
        List<SysMusicData> list = sysMusicDataService.selectSysMusicDataList(sysMusicData);
        ExcelUtil<SysMusicData> util = new ExcelUtil<SysMusicData>(SysMusicData.class);
        util.exportExcel(response, list, "纯音乐享数据");
    }

    /**
     * 获取纯音乐享详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:musicData:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysMusicDataService.selectSysMusicDataById(id));
    }

    /**
     * 新增纯音乐享
     */
    @PreAuthorize("@ss.hasPermi('system:musicData:add')")
    @Log(title = "纯音乐享", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMusicData sysMusicData)
    {
        return toAjax(sysMusicDataService.insertSysMusicData(sysMusicData));
    }

    /**
     * 修改纯音乐享
     */
    @PreAuthorize("@ss.hasPermi('system:musicData:edit')")
    @Log(title = "纯音乐享", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMusicData sysMusicData)
    {
        return toAjax(sysMusicDataService.updateSysMusicData(sysMusicData));
    }

    /**
     * 删除纯音乐享
     */
    @PreAuthorize("@ss.hasPermi('system:musicData:remove')")
    @Log(title = "纯音乐享", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            SysMusicData sysMusicData = sysMusicDataService.selectSysMusicDataById(id);
            Integer souceType = sysMusicData.getSouceType();
            if (souceType == SourceTypeEnum.synch.getCode() || souceType == SourceTypeEnum.usb.getCode()){
                String videoAddress = sysMusicData.getVideoAddress();
                String lyricAddress = sysMusicData.getLyricAddress();
                File musicFile = new File(videoAddress);
                if (musicFile.exists()) {
                    boolean delete = musicFile.delete();
                    logger.info("删除歌曲资源:"+delete+",地址:"+videoAddress);
                }
                File lyricFile = new File(lyricAddress);
                if (lyricFile.exists()) {
                    boolean delete = lyricFile.delete();
                    logger.info("删除歌词文件:"+delete+",地址:"+lyricAddress);
                }
            }
            logger.info("删除歌曲数据来源:"+souceType);
            sysMusicDataService.deleteSysMusicDataById(id);
        }
        return success();
    }
}
